
function ajax(_method, _url, _callBack, _data) {
    return new Promise(function (resolve, reject) {
        let xhr = new XMLHttpRequest();
        let result; // 获取异步数据
        // http://127.0.0.1:5500/test.html?name=hy&id=1&
        // 处理get请求, 参数放置请求头
        let str = "?";
        // 如果有传入数据就进行处理
        if (_data) {
            for (let key in _data) {
                str += `${key}=${_data[key]}&`;
            };
        }
        // ?name=hy&id=1

        // 判断是否是get请求
        if (_method.toUpperCase() == "GET") {
            // console.log("这是一个get请求");
            xhr.open(_method, _url + str.slice(0, -1));
            xhr.send();
        } else {
            // post及其他请求
            // console.log("这是一个post请求");
            xhr.open(_method, _url);
            xhr.setRequestHeader("Content-Type", "application/json");
            xhr.send(JSON.stringify(_data));
        }

        xhr.addEventListener("readystatechange", function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    result = JSON.parse(xhr.responseText);
                    resolve(_callBack(result));

                } else {
                    result = JSON.parse(xhr.responseText);
                    reject(_callBack(result));
                }
                // 将结果通过函数的参数返回出去
                // _callBack(result);
            }
        });
    })
}




//开始预约
function af1() {
    return ajax("post", "http://newshopapi.0melon0.cn/api/f_user/register", function (res) {
        // console.log(res);
        // console.log(res.result.token);
        sessionStorage.setItem("token", String(res.result.token));
        // document.cookie = 'token' + "=" + String(res.result.token); 
        return res;
    }, {
        name: sessionStorage.getItem("name"),
        phone: sessionStorage.getItem("phone"),
        card: sessionStorage.getItem("card")
        // name: "hh",
        // phone: "13535970127",
        // card: "441823199705121234"
    });
}

//获取预约时间列表
function af2(a) {
    return ajax("get", "http://newshopapi.0melon0.cn/api/f_user/chooselist", function (res) {
        // console.log(res);

        let arr = res.result.list;
        //遍历日期
        arr.forEach(function (item, index) {
            xiala.innerHTML += `<option value="${item.day_time}">${moment(item.day_time).format('MM月-DD日')}</option> `;
            // console.log(moment(item.day_time).format('MM月DD日'));
        })
        // console.log(res);
        return res;

    }, {
        // token: sessionStorage.getItem("token")
        token: a
        // token:"2a8da3f2df4942ff96fca1ae757480"

    });
}

//查询某天场次
function af3(time) {


    return ajax("get", "http://newshopapi.0melon0.cn/api/f_user/chooseday", function (res) {
        // console.log(res);

        let arr = res.result.list;
        arr.forEach(function (item, index) {
            let oneTime = item.show_name.slice(0, 5);
            let twoTime = item.show_name.slice(6, 11);
            let cc = xiala.value;
            // console.log(oneTime);
            //判断当前时间是否超过预约时间

            if (date.isDuringDate(cc, oneTime, twoTime)) {
                sure = "可预约"
            } else {
                sure = "已约满"
            }
            tbody.innerHTML += `<tr><td>${item.show_name}</td><td class="left" data-day_time="${item.day_time}" data-show_id="${item.show_id}">${sure}</td></tr> `;
        })

        return res;
    }, {
        token: sessionStorage.getItem("token"),
        // token: document.cookie.split("=")[1],

        day_time: time
    });



}


//预约某天场次

function af4() {
    return ajax("post", "http://newshopapi.0melon0.cn/api/f_user/bookday", function (res) {
        // console.log(res);
        // console.log(res.result.name);
        // console.log(sessionStorage.getItem("dd"));
        //名字
        userName.innerHTML = res.result.name
        dd.innerHTML = sessionStorage.getItem("dd")
        sessionStorage.setItem("id", res.result.id);
    }, {
        token: sessionStorage.getItem("token"),
        day_time: sessionStorage.getItem("day_time"),
        show_id: sessionStorage.getItem("show_id")

    })
}

//获取二维码
function af5() {
    return ajax("get", "http://newshopapi.0melon0.cn/api/f_user/Qrcode", function (res) {
        // console.log(res);
        img.src = res.data.data;

        btn.addEventListener("click", function () {

            let a = document.createElement('a'),
                event = new MouseEvent('click')
            a.download = '二维码';
            a.href = res.data.data;
            a.dispatchEvent(event)
        })
    }, {
        token: sessionStorage.getItem("token"),
        id: sessionStorage.getItem("id")

    })
}

//获取预约记录
function af6() {
    return ajax("post", "http://newshopapi.0melon0.cn/api/f_user/report", function (res) {
        //   console.log(res.result[res.result.length-1]);
        console.log(res);
        let aaa = res.result[res.result.length - 1]
        th[0].innerHTML = "姓名："+aaa.name;
        th[1].innerHTML = "日期："+aaa.day_time;

        return res
    }, {
        name: sessionStorage.getItem("name"),
        phone: sessionStorage.getItem("phone")
    });
}


//查询某天场次
function af6_1(time, a) {


    return ajax("get", "http://newshopapi.0melon0.cn/api/f_user/chooseday", function (res) {
        console.log(res);
        let arr = res.result.list;
        arr.forEach((e) => {
            if (e.show_id == a) {
                th[2].innerHTML ="场次：" +e.show_name;
            }
        })
        return res;
    }, {
        token: sessionStorage.getItem("token"),
        // token: document.cookie.split("=")[1],

        day_time: time
    });
}

//获取二维码
function af6_2(a) {
    return ajax("get", "http://newshopapi.0melon0.cn/api/f_user/Qrcode", function (res) {
        console.log(res);
        img.src = res.data.data;


    }, {
        token: sessionStorage.getItem("token"),
        id: a

    })
}
let gg = document.querySelector(".gg")
gg.addEventListener("click",()=>{
    window.open("./index.html", '_self')
})